package com.boyuan.huijiaoyu;

import java.util.ArrayList;
import java.util.List;

import com.example.contactfragment.ContactService;
import com.example.huanxin.AlertDialog;
import com.example.huanxin.ChatActivity;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;

import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyfriendPickActivity extends Activity implements OnClickListener{

	private ListView listview;
	private ContactService contactService;
	private ArrayList<DetailPerson> friendlist = new ArrayList<DetailPerson>();
	private ArrayList<String> mobilelist = new ArrayList<String>();
	private ArrayList<String> namelist = new ArrayList<String>();
	private ArrayList<String> urllist = new ArrayList<String>();
	private MyAdapter adapter;
	private String id;
	private String toUser;
	private String forward_msg_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myfriend_pick);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		id = sp.getString("id", "");
		forward_msg_id = getIntent().getStringExtra("forward_msg_id");
		getMyFriendPickCache();
		getFrequentContacts();
		initUI();
	}

	private void initUI() {
		findViewById(R.id.back_pick).setOnClickListener(this);
		listview = (ListView) findViewById(R.id.listView_pick);
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int possion,
					long arg3) {
				toUser = mobilelist.get(possion);
				Intent intent = new Intent(MyfriendPickActivity.this, AlertDialog.class);
				intent.putExtra("cancel", true);
				intent.putExtra("titleIsCancel", true);
				intent.putExtra("msg", getString(R.string.confirm_forward_to, toUser));
				startActivityForResult(intent , 1);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			try {
				ChatActivity.activityInstance.finish();
			} catch (Exception e) {
			}
			Intent intent = new Intent(this, ChatActivity.class);
			if (toUser == null)
				return;
			intent.putExtra("userId", toUser);
			intent.putExtra("forward_msg_id", forward_msg_id);
			startActivity(intent);
			finish();

		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return namelist.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View inflate = getLayoutInflater().inflate(R.layout.list_myfriendpick, null);
			TextView tv = (TextView) inflate.findViewById(R.id.tv_pickname);
			tv.setText(namelist.get(arg0));
			return inflate;
		}
		
	}
	
	//获取我的好友缓存
	private void getMyFriendPickCache() {
		friendlist.clear();
		mobilelist.clear();
		namelist.clear();
		urllist.clear();
		ArrayList<DetailPerson> myfriendList = SharePreferenceHelper.getMyFrindList(MyfriendPickActivity.this, id);
		if (myfriendList.size() > 0) {
			friendlist.addAll(myfriendList);
			for (int i = 0; i < friendlist.size(); i++) {
				mobilelist.add(friendlist.get(i).getMobile());
				namelist.add(friendlist.get(i).getRealname());
				urllist.add(Constant.photourl + friendlist.get(i).getMobile());
			}
		}
	}
	
	/**
	 * 获取常用联系人
	 */
	private void getFrequentContacts() {

		if (contactService == null) {
			contactService = new ContactService(this);
		}
		contactService.getFrequentContacts(id,
				new GetOneRecordListener<List<DetailPerson>>() {

					@Override
					public void onFinish(List<DetailPerson> data) {
						friendlist.clear();
						mobilelist.clear();
						namelist.clear();
						urllist.clear();
						if (data != null && data.size() > 0) {
							friendlist.addAll(data);
							SharePreferenceHelper.saveMyFrindPickList(MyfriendPickActivity.this, id, data);
							for (int i = 0; i < friendlist.size(); i++) {
								mobilelist.add(friendlist.get(i).getMobile());
								namelist.add(friendlist.get(i).getRealname());
								urllist.add(Constant.photourl + friendlist.get(i).getMobile());
							}

						} else {
							Toast.makeText(MyfriendPickActivity.this, "您还没有好友！",
									Toast.LENGTH_SHORT).show();
							finish();
						}
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onFailed(String Message) {
						ToastHelper.showMsg(MyfriendPickActivity.this, Message,
								false);
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.myfriend_pick, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_pick:
			finish();
			break;

		default:
			break;
		}
	}


}
